package com.ztf.room.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztf.room.bean.Evaluates;
import com.ztf.room.bean.Student;
import com.ztf.room.bean.Teachers;
import com.ztf.room.dao.StudentDao;
import com.ztf.room.dao.TeacherDao;
import com.ztf.room.exception.BusinessException;
import com.ztf.room.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class TeacherServiceImp implements TeacherService {

    @Autowired
    private TeacherDao teacherDao;

    @Autowired
    private StudentDao studentDao;

    // 学生提交老师评价
    @Override
    public boolean saveEvaluation(Evaluates evaluates) {
        Teachers teacher = teacherDao.selectTeacherById(evaluates.getTeacherId());
        if (teacher==null){
            throw new BusinessException("该教师不存在");
        }
        int i = teacherDao.saveEvaluation(evaluates);
        if (i==0){
            throw new BusinessException("错误评价");
        }
        return true;
    }

    @Override
    public Teachers login(String account, String password) {
        Teachers teachers = teacherDao.login(account,password);
        if (teachers==null){
            throw new BusinessException("密码或账号错误");
        }
        return teachers;
    }

    @Override
    public PageInfo<Evaluates> getEvauatesByStu(Integer studentId, int page, int limit) {
        PageHelper.startPage(page,limit);
        // 获取到的是
        List<Evaluates> list =  teacherDao.getEvauatesByStu(studentId);
        return PageInfo.of(list);
    }

    @Override
    public PageInfo<Evaluates> getEvauatesByTea(Integer id, int page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<Evaluates> list =  teacherDao.getEvauatesByTea(id);
        return PageInfo.of(list);
    }

    @Override
    public PageInfo<Teachers> getTeachers(int page, int limit) {
        PageHelper.startPage(page,limit);
        List<Teachers> list =  teacherDao.getTeachers();

        return PageInfo.of(list);
    }

    @Transactional
    @Override
    public boolean releaseEval(ArrayList<Teachers> list) {
        // 发布
        List<Student> students = studentDao.selectAll();
        for (int i = 0; i < list.size(); i++) {
            // 给每个老师生成一个
            teacherDao.insertRelease(list.get(i).getTeacherId(),list.get(i).getTeacherName(),students);
        }
        return true;
    }
}
